package com.example.design.deprecated.state;

import com.example.design.utils.RedisUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class DeprecatedPayOrder extends DeprecatedAbstractOrderState{

    @Resource
    private RedisUtils redisUtils;

    @Resource
    private DeprecatedSendOrder sendOrder;

    @Override
    public DeprecatedOrder payOrder(String orderId, DeprecatedOrderContext context) {
        //订单支付成功后，将订单状态太改为待发货
        DeprecatedOrder order = (DeprecatedOrder) redisUtils.get(orderId);
        if (!order.getState().equals(ORDER_WAIT_PAY)){
            throw new UnsupportedOperationException("Order state should be ORDER_WAIT_PAY" +
                    ".But now it's state is : " + order.getState());
        }
        //支付逻辑（第五章）
        //支付完成后，将订单状态更新为待发货
        order.setState(ORDER_WAIT_SEND);
        redisUtils.set(orderId,order);
        //观察者模式：发送订单支付成功消息
        //订单支付完成，设置Context上下文状态为发货状态
        super.notifyObserver(orderId,ORDER_WAIT_SEND);
        return order;
    }
}
